package org.destiny.account.dao;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class DaoFactory {
	private static SqlSessionFactory sessionFactory;
	private static SqlSession session;
	private static CustomerDao customerDao;

	static {
		String resource = "mybatis.cfg.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			sessionFactory = builder.build(reader);
			session = sessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static CustomerDao getCustomerDao() {
		if (customerDao == null)
			customerDao = session.getMapper(CustomerDao.class);
		return customerDao;
	}

	public static SqlSession getSession() {
		return session;
	}

}
